{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "synthetic_features_and_outliers.ipynb",
      "version": "0.3.2",
      "views": {},
      "default_view": {},
      "provenance": [],
      "collapsed_sections": [
        "i5Ul3zf5QYvW",
        "jByCP8hDRZmM",
        "WvgxW0bUSC-c",
        "copyright-notice"
      ]
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "source": [
        "#### Copyright 2017 Google LLC."
      ],
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "copyright-notice"
      }
    },
    {
      "source": [
        "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": [],
      "metadata": {
        "colab_type": "code",
        "id": "copyright-notice2",
        "colab": {
          "autoexec": {
            "wait_interval": 0,
            "startup": false
          }
        },
        "cellView": "both"
      }
    },
    {
      "metadata": {
        "id": "4f3CKqFUqL2-",
        "colab_type": "text",
        "slideshow": {
          "slide_type": "slide"
        }
      },
      "cell_type": "markdown",
      "source": [
        " # \ud569\uc131 \ud2b9\uc131\uacfc \uc774\uc0c1\uc810"
      ]
    },
    {
      "metadata": {
        "id": "jnKgkN5fHbGy",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " **\ud559\uc2b5 \ubaa9\ud45c:**\n",
        "  * \ub2e4\ub978 \ub450 \ud2b9\uc131\uc758 \ube44\uc728\ub85c \ud569\uc131 \ud2b9\uc131\uc744 \ub9cc\ub4e0\ub2e4\n",
        "  * \uc0c8 \ud2b9\uc131\uc744 \uc120\ud615 \ud68c\uadc0 \ubaa8\ub378\uc758 \uc785\ub825\uc73c\ub85c \uc0ac\uc6a9\ud55c\ub2e4\n",
        "  * \uc785\ub825 \ub370\uc774\ud130\uc5d0\uc11c \uc774\uc0c1\uc810\uc744 \uc2dd\ubcc4 \ubc0f \uc0ad\uc81c\ud558\uc5ec \ubaa8\ub378\uc758 \ud6a8\uc728\uc131\uc744 \uac1c\uc120\ud55c\ub2e4"
      ]
    },
    {
      "metadata": {
        "id": "VOpLo5dcHbG0",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \ud150\uc11c\ud50c\ub85c\uc6b0 \uccab\uac78\uc74c \uc2e4\uc2b5\uc5d0\uc11c \uc0ac\uc6a9\ud55c \ubaa8\ub378\uc744 \ub2e4\uc2dc \uc0b4\ud3b4\ubcf4\uaca0\uc2b5\ub2c8\ub2e4.\n",
        "\n",
        "\uc6b0\uc120 \uce98\ub9ac\ud3ec\ub2c8\uc544 \uc8fc\ud0dd \ub370\uc774\ud130\ub97c *pandas* `DataFrame`\uc73c\ub85c \uac00\uc838\uc635\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "S8gm6BpqRRuh",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " ## \uc124\uc815"
      ]
    },
    {
      "metadata": {
        "id": "9D8GgUovHbG0",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "from __future__ import print_function\n",
        "\n",
        "import math\n",
        "\n",
        "from IPython import display\n",
        "from matplotlib import cm\n",
        "from matplotlib import gridspec\n",
        "import matplotlib.pyplot as plt\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "import sklearn.metrics as metrics\n",
        "import tensorflow as tf\n",
        "from tensorflow.python.data import Dataset\n",
        "\n",
        "tf.logging.set_verbosity(tf.logging.ERROR)\n",
        "pd.options.display.max_rows = 10\n",
        "pd.options.display.float_format = '{:.1f}'.format\n",
        "\n",
        "california_housing_dataframe = pd.read_csv(\"https://download.mlcc.google.com/mledu-datasets/california_housing_train.csv\", sep=\",\")\n",
        "\n",
        "california_housing_dataframe = california_housing_dataframe.reindex(\n",
        "    np.random.permutation(california_housing_dataframe.index))\n",
        "california_housing_dataframe[\"median_house_value\"] /= 1000.0\n",
        "california_housing_dataframe"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "I6kNgrwCO_ms",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \ub2e4\uc74c\uc73c\ub85c, \uc785\ub825 \ud568\uc218\ub97c \uc124\uc815\ud558\uace0 \ubaa8\ub378 \ud559\uc2b5\uc6a9 \ud568\uc218\ub97c \uc815\uc758\ud569\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "5RpTJER9XDub",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "def my_input_fn(features, targets, batch_size=1, shuffle=True, num_epochs=None):\n",
        "    \"\"\"Trains a linear regression model of one feature.\n",
        "  \n",
        "    Args:\n",
        "      features: pandas DataFrame of features\n",
        "      targets: pandas DataFrame of targets\n",
        "      batch_size: Size of batches to be passed to the model\n",
        "      shuffle: True or False. Whether to shuffle the data.\n",
        "      num_epochs: Number of epochs for which data should be repeated. None = repeat indefinitely\n",
        "    Returns:\n",
        "      Tuple of (features, labels) for next data batch\n",
        "    \"\"\"\n",
        "    \n",
        "    # Convert pandas data into a dict of np arrays.\n",
        "    features = {key:np.array(value) for key,value in dict(features).items()}                                           \n",
        " \n",
        "    # Construct a dataset, and configure batching/repeating.\n",
        "    ds = Dataset.from_tensor_slices((features,targets)) # warning: 2GB limit\n",
        "    ds = ds.batch(batch_size).repeat(num_epochs)\n",
        "    \n",
        "    # Shuffle the data, if specified.\n",
        "    if shuffle:\n",
        "      ds = ds.shuffle(buffer_size=10000)\n",
        "    \n",
        "    # Return the next batch of data.\n",
        "    features, labels = ds.make_one_shot_iterator().get_next()\n",
        "    return features, labels"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "VgQPftrpHbG3",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "def train_model(learning_rate, steps, batch_size, input_feature):\n",
        "  \"\"\"Trains a linear regression model.\n",
        "  \n",
        "  Args:\n",
        "    learning_rate: A `float`, the learning rate.\n",
        "    steps: A non-zero `int`, the total number of training steps. A training step\n",
        "      consists of a forward and backward pass using a single batch.\n",
        "    batch_size: A non-zero `int`, the batch size.\n",
        "    input_feature: A `string` specifying a column from `california_housing_dataframe`\n",
        "      to use as input feature.\n",
        "      \n",
        "  Returns:\n",
        "    A Pandas `DataFrame` containing targets and the corresponding predictions done\n",
        "    after training the model.\n",
        "  \"\"\"\n",
        "  \n",
        "  periods = 10\n",
        "  steps_per_period = steps / periods\n",
        "\n",
        "  my_feature = input_feature\n",
        "  my_feature_data = california_housing_dataframe[[my_feature]].astype('float32')\n",
        "  my_label = \"median_house_value\"\n",
        "  targets = california_housing_dataframe[my_label].astype('float32')\n",
        "\n",
        "  # Create input functions.\n",
        "  training_input_fn = lambda: my_input_fn(my_feature_data, targets, batch_size=batch_size)\n",
        "  predict_training_input_fn = lambda: my_input_fn(my_feature_data, targets, num_epochs=1, shuffle=False)\n",
        "  \n",
        "  # Create feature columns.\n",
        "  feature_columns = [tf.feature_column.numeric_column(my_feature)]\n",
        "    \n",
        "  # Create a linear regressor object.\n",
        "  my_optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)\n",
        "  my_optimizer = tf.contrib.estimator.clip_gradients_by_norm(my_optimizer, 5.0)\n",
        "  linear_regressor = tf.estimator.LinearRegressor(\n",
        "      feature_columns=feature_columns,\n",
        "      optimizer=my_optimizer\n",
        "  )\n",
        "\n",
        "  # Set up to plot the state of our model's line each period.\n",
        "  plt.figure(figsize=(15, 6))\n",
        "  plt.subplot(1, 2, 1)\n",
        "  plt.title(\"Learned Line by Period\")\n",
        "  plt.ylabel(my_label)\n",
        "  plt.xlabel(my_feature)\n",
        "  sample = california_housing_dataframe.sample(n=300)\n",
        "  plt.scatter(sample[my_feature], sample[my_label])\n",
        "  colors = [cm.coolwarm(x) for x in np.linspace(-1, 1, periods)]\n",
        "\n",
        "  # Train the model, but do so inside a loop so that we can periodically assess\n",
        "  # loss metrics.\n",
        "  print(\"Training model...\")\n",
        "  print(\"RMSE (on training data):\")\n",
        "  root_mean_squared_errors = []\n",
        "  for period in range (0, periods):\n",
        "    # Train the model, starting from the prior state.\n",
        "    linear_regressor.train(\n",
        "        input_fn=training_input_fn,\n",
        "        steps=steps_per_period,\n",
        "    )\n",
        "    # Take a break and compute predictions.\n",
        "    predictions = linear_regressor.predict(input_fn=predict_training_input_fn)\n",
        "    predictions = np.array([item['predictions'][0] for item in predictions])\n",
        "    \n",
        "    # Compute loss.\n",
        "    root_mean_squared_error = math.sqrt(\n",
        "      metrics.mean_squared_error(predictions, targets))\n",
        "    # Occasionally print the current loss.\n",
        "    print(\"  period %02d : %0.2f\" % (period, root_mean_squared_error))\n",
        "    # Add the loss metrics from this period to our list.\n",
        "    root_mean_squared_errors.append(root_mean_squared_error)\n",
        "    # Finally, track the weights and biases over time.\n",
        "    # Apply some math to ensure that the data and line are plotted neatly.\n",
        "    y_extents = np.array([0, sample[my_label].max()])\n",
        "    \n",
        "    weight = linear_regressor.get_variable_value('linear/linear_model/%s/weights' % input_feature)[0]\n",
        "    bias = linear_regressor.get_variable_value('linear/linear_model/bias_weights')\n",
        "    \n",
        "    x_extents = (y_extents - bias) / weight\n",
        "    x_extents = np.maximum(np.minimum(x_extents,\n",
        "                                      sample[my_feature].max()),\n",
        "                           sample[my_feature].min())\n",
        "    y_extents = weight * x_extents + bias\n",
        "    plt.plot(x_extents, y_extents, color=colors[period]) \n",
        "  print(\"Model training finished.\")\n",
        "\n",
        "  # Output a graph of loss metrics over periods.\n",
        "  plt.subplot(1, 2, 2)\n",
        "  plt.ylabel('RMSE')\n",
        "  plt.xlabel('Periods')\n",
        "  plt.title(\"Root Mean Squared Error vs. Periods\")\n",
        "  plt.tight_layout()\n",
        "  plt.plot(root_mean_squared_errors)\n",
        "\n",
        "  # Create a table with calibration data.\n",
        "  calibration_data = pd.DataFrame()\n",
        "  calibration_data[\"predictions\"] = pd.Series(predictions)\n",
        "  calibration_data[\"targets\"] = pd.Series(targets)\n",
        "  display.display(calibration_data.describe())\n",
        "\n",
        "  print(\"Final RMSE (on training data): %0.2f\" % root_mean_squared_error)\n",
        "  \n",
        "  return calibration_data"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "FJ6xUNVRm-do",
        "colab_type": "text",
        "slideshow": {
          "slide_type": "slide"
        }
      },
      "cell_type": "markdown",
      "source": [
        " ## \uc791\uc5c5 1: \ud569\uc131 \ud2b9\uc131 \uc0ac\uc6a9\ud574 \ubcf4\uae30\n",
        "\n",
        "`total_rooms` \ud2b9\uc131\uacfc `population` \ud2b9\uc131\uc740 \ubaa8\ub450 \ud2b9\uc815 \uc9c0\uc5ed\uc758 \ud569\uacc4\ub97c \uacc4\uc218\ud569\ub2c8\ub2e4.\n",
        "\n",
        "\uadf8\ub7f0\ub370 \uc9c0\uc5ed\ub9c8\ub2e4 \uc778\uad6c\ubc00\ub3c4\uac00 \ub2e4\ub974\ub2e4\uba74 \uc5b4\ub5bb\uac8c \ub420\uae4c\uc694? `total_rooms`\uc640 `population`\uc758 \ube44\uc728\ub85c \ud569\uc131 \ud2b9\uc131\uc744 \ub9cc\ub4e4\uba74 \uc9c0\uc5ed\uc758 \uc778\uad6c\ubc00\ub3c4\uc640 \uc8fc\ud0dd \uac00\uaca9 \uc911\uc559\uac12\uc758 \uad00\uacc4\ub97c \uc0b4\ud3b4\ubcfc \uc218 \uc788\uc2b5\ub2c8\ub2e4.\n",
        "\n",
        "\uc544\ub798 \uc140\uc5d0\uc11c `rooms_per_person`\uc774\ub77c\ub294 \ud2b9\uc131\uc744 \ub9cc\ub4e4\uace0 `train_model()`\uc758 `input_feature`\ub85c \uc0ac\uc6a9\ud569\ub2c8\ub2e4.\n",
        "\n",
        "\ud559\uc2b5\ub960\uc744 \uc870\uc815\ud558\uc5ec \uc774 \ub2e8\uc77c \ud2b9\uc131\uc73c\ub85c \uc131\ub2a5\uc744 \uc5b4\ub514\uae4c\uc9c0 \uc62c\ub9b4 \uc218 \uc788\uc744\uae4c\uc694? \uc131\ub2a5\uc774 \ub192\ub2e4\ub294 \uac83\uc740 \ud68c\uadc0\uc120\uc774 \ub370\uc774\ud130\uc5d0 \uc798 \ubd80\ud569\ud558\uace0 \ucd5c\uc885 RMSE\uac00 \ub0ae\ub2e4\ub294 \uc758\ubbf8\uc785\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "isONN2XK32Wo",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " **\ucc38\uace0**: \uc544\ub798\uc5d0 \ucf54\ub4dc \uc140\uc744 \uba87 \uac1c \ucd94\uac00\ud558\uc5ec \ub2e4\uc591\ud55c \ud559\uc2b5\ub960\uc744 \uc2e4\ud5d8\ud558\uba74\uc11c \uacb0\uacfc\ub97c \ube44\uad50\ud574 \ubcf4\uba74 \ub3c4\uc6c0\uc774 \ub429\ub2c8\ub2e4. \uc0c8 \ucf54\ub4dc \uc140\uc744 \ucd94\uac00\ud558\ub824\uba74 \uc774 \uc140 \uac00\uc6b4\ub370 \ubc14\ub85c \uc544\ub798\uc5d0 \ub9c8\uc6b0\uc2a4\ub97c \uac00\uc838\uac00\uace0 **CODE**\ub97c \ud074\ub9ad\ud569\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "5ihcVutnnu1D",
        "colab_type": "code",
        "slideshow": {
          "slide_type": "slide"
        },
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "test": {
            "output": "ignore",
            "timeout": 600
          }
        },
        "cellView": "both"
      },
      "source": [
        "#\n",
        "# YOUR CODE HERE\n",
        "#\n",
        "california_housing_dataframe[\"rooms_per_person\"] =\n",
        "\n",
        "calibration_data = train_model(\n",
        "    learning_rate=0.00005,\n",
        "    steps=500,\n",
        "    batch_size=5,\n",
        "    input_feature=\"rooms_per_person\"\n",
        ")"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "i5Ul3zf5QYvW",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " ### \ud574\uacb0 \ubc29\ubc95\n",
        "\n",
        "\ud574\uacb0 \ubc29\ubc95\uc744 \ubcf4\ub824\uba74 \uc544\ub798\ub97c \ud074\ub9ad\ud558\uc138\uc694."
      ]
    },
    {
      "metadata": {
        "id": "Leaz2oYMQcBf",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "california_housing_dataframe[\"rooms_per_person\"] = (\n",
        "    california_housing_dataframe[\"total_rooms\"] / california_housing_dataframe[\"population\"])\n",
        "\n",
        "calibration_data = train_model(\n",
        "    learning_rate=0.05,\n",
        "    steps=500,\n",
        "    batch_size=5,\n",
        "    input_feature=\"rooms_per_person\")"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "ZjQrZ8mcHFiU",
        "colab_type": "text",
        "slideshow": {
          "slide_type": "slide"
        }
      },
      "cell_type": "markdown",
      "source": [
        " ## \uc791\uc5c5 2: \uc774\uc0c1\uc810 \uc2dd\ubcc4\n",
        "\n",
        "\uc608\uce21\uacfc \ubaa9\ud45c\uac12\uc744 \ube44\uad50\ud55c \uc0b0\ud3ec\ub3c4\ub97c \uc791\uc131\ud558\uba74 \ubaa8\ub378\uc758 \uc131\ub2a5\uc744 \uc2dc\uac01\ud654\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.  \uc774\uc0c1\uc801\uc778 \uc0c1\ud0dc\ub294 \uc644\ubcbd\ud55c \uc0c1\uad00\uc131\uc744 \uac16\ub294 \ub300\uac01\uc120\uc774 \uadf8\ub824\uc9c0\ub294 \uac83\uc785\ub2c8\ub2e4.\n",
        "\n",
        "\uc791\uc5c5 1\uc5d0\uc11c \ud559\uc2b5\ud55c rooms-per-person \ubaa8\ub378\uc744 \uc0ac\uc6a9\ud55c \uc608\uce21\uacfc \ud0c0\uac9f\uc5d0 \ub300\ud574 Pyplot\uc758 `scatter()`\ub85c \uc0b0\ud3ec\ub3c4\ub97c \uc791\uc131\ud569\ub2c8\ub2e4.\n",
        "\n",
        "\ud2b9\uc774\ud55c \uc810\uc774 \ub208\uc5d0 \ub744\ub098\uc694? `rooms_per_person`\uc758 \uac12 \ubd84\ud3ec\ub97c \uc870\uc0ac\ud558\uc5ec \uc18c\uc2a4 \ub370\uc774\ud130\ub97c \ucd94\uc801\ud574 \ubcf4\uc138\uc694."
      ]
    },
    {
      "metadata": {
        "id": "P0BDOec4HbG_",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "# YOUR CODE HERE"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "jByCP8hDRZmM",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " ### \ud574\uacb0 \ubc29\ubc95\n",
        "\n",
        "\ud574\uacb0 \ubc29\ubc95\uc744 \ubcf4\ub824\uba74 \uc544\ub798\ub97c \ud074\ub9ad\ud558\uc138\uc694."
      ]
    },
    {
      "metadata": {
        "id": "s0tiX2gdRe-S",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "plt.figure(figsize=(15, 6))\n",
        "plt.subplot(1, 2, 1)\n",
        "plt.scatter(calibration_data[\"predictions\"], calibration_data[\"targets\"])"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "kMQD0Uq3RqTX",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \ubcf4\uc815 \ub370\uc774\ud130\ub97c \ubcf4\uba74 \ub300\ubd80\ubd84\uc758 \uc0b0\ud3ec\uc810\uc774 \uc9c1\uc120\uc744 \uc774\ub8f9\ub2c8\ub2e4. \uc774 \uc120\uc740 \uc218\uc9c1\uc5d0 \uac00\uae4c\uc6b4\ub370, \uc5ec\uae30\uc5d0 \ub300\ud574\uc11c\ub294 \ub098\uc911\uc5d0 \uc124\uba85\ud569\ub2c8\ub2e4. \uc9c0\uae08\uc740 \uc120\uc5d0\uc11c \ubc97\uc5b4\ub09c \uc810\uc5d0 \ub300\ud574 \uc9d1\uc911\ud560 \ub54c\uc785\ub2c8\ub2e4. \uc774\ub7ec\ud55c \uc810\uc740 \ube44\uad50\uc801 \uc801\uc740 \ud3b8\uc785\ub2c8\ub2e4.\n",
        "\n",
        "`rooms_per_person`\uc758 \ud788\uc2a4\ud1a0\uadf8\ub7a8\uc744 \uadf8\ub824\ubcf4\uba74 \uc785\ub825 \ub370\uc774\ud130\uc5d0\uc11c \uba87 \uac1c\uc758 \uc774\uc0c1\uc810\uc744 \ubc1c\uacac\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "POTM8C_ER1Oc",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "plt.subplot(1, 2, 2)\n",
        "_ = california_housing_dataframe[\"rooms_per_person\"].hist()"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "9l0KYpBQu8ed",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " ## \uc791\uc5c5 3: \uc774\uc0c1\uc810 \uc0ad\uc81c\n",
        "\n",
        "`rooms_per_person`\uc758 \uc774\uc0c1\uc810 \uac12\uc744 \uc801\ub2f9\ud55c \ucd5c\uc18c\uac12 \ub610\ub294 \ucd5c\ub300\uac12\uc73c\ub85c \uc124\uc815\ud558\uc5ec \ubaa8\ub378\uc758 \uc801\ud569\uc131\uc744 \ub354 \ub192\uc77c \uc218 \uc788\ub294\uc9c0 \uc0b4\ud3b4\ubcf4\uc138\uc694.\n",
        "\n",
        "\ub2e4\uc74c\uc740 Pandas `Series`\uc5d0 \ud568\uc218\ub97c \uc801\uc6a9\ud558\ub294 \ubc29\ubc95\uc744 \uac04\ub2e8\ud788 \ubcf4\uc5ec\uc8fc\ub294 \uc608\uc81c\uc785\ub2c8\ub2e4.\n",
        "\n",
        "    clipped_feature = my_dataframe[\"my_feature_name\"].apply(lambda x: max(x, 0))\n",
        "\n",
        "\uc704\uc640 \uac19\uc740 `clipped_feature`\ub294 `0` \ubbf8\ub9cc\uc758 \uac12\uc744 \ud3ec\ud568\ud558\uc9c0 \uc54a\uc2b5\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "rGxjRoYlHbHC",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "# YOUR CODE HERE"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "WvgxW0bUSC-c",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " ### \ud574\uacb0 \ubc29\ubc95\n",
        "\n",
        "\ud574\uacb0 \ubc29\ubc95\uc744 \ubcf4\ub824\uba74 \uc544\ub798\ub97c \ud074\ub9ad\ud558\uc138\uc694."
      ]
    },
    {
      "metadata": {
        "id": "8YGNjXPaSMPV",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \uc791\uc5c5 2\uc5d0\uc11c \uc791\uc131\ud55c \ud788\uc2a4\ud1a0\uadf8\ub7a8\uc744 \ubcf4\uba74 \ub300\ubd80\ubd84\uc758 \uac12\uc774 `5` \ubbf8\ub9cc\uc785\ub2c8\ub2e4. `rooms_per_person`\uc744 5\uc5d0\uc11c \uc798\ub77c\ub0b4\uace0 \ud788\uc2a4\ud1a0\uadf8\ub7a8\uc744 \uc791\uc131\ud558\uc5ec \uacb0\uacfc\ub97c \ub2e4\uc2dc \ud655\uc778\ud574 \ubcf4\uc138\uc694."
      ]
    },
    {
      "metadata": {
        "id": "9YyARz6gSR7Q",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "california_housing_dataframe[\"rooms_per_person\"] = (\n",
        "    california_housing_dataframe[\"rooms_per_person\"]).apply(lambda x: min(x, 5))\n",
        "\n",
        "_ = california_housing_dataframe[\"rooms_per_person\"].hist()"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "vO0e1p_aSgKA",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \uc0ad\uc81c\uac00 \ud6a8\uacfc\uac00 \uc788\uc5c8\ub294\uc9c0 \ud655\uc778\ud558\uae30 \uc704\ud574 \ud559\uc2b5\uc744 \ub2e4\uc2dc \uc2e4\ud589\ud558\uace0 \ubcf4\uc815 \ub370\uc774\ud130\ub97c \ud55c \ubc88 \ub354 \ucd9c\ub825\ud574 \ubcf4\uaca0\uc2b5\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "ZgSP2HKfSoOH",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "calibration_data = train_model(\n",
        "    learning_rate=0.05,\n",
        "    steps=500,\n",
        "    batch_size=5,\n",
        "    input_feature=\"rooms_per_person\")"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "gySE-UgfSony",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "_ = plt.scatter(calibration_data[\"predictions\"], calibration_data[\"targets\"])"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    }
  ]
}
